// 当页面加载完成时，加载已保存的配置
document.addEventListener('DOMContentLoaded', () => {
  // 从 chrome.storage 中加载已保存的配置
  chrome.storage.sync.get(['modelUrl', 'modelName', 'apiKey'], (result) => {
    document.getElementById('modelUrl').value = result.modelUrl || '';
    document.getElementById('modelName').value = result.modelName || '';
    document.getElementById('apiKey').value = result.apiKey || '';
  });
  
  // 添加保存按钮的点击事件监听器
  document.getElementById('saveButton').addEventListener('click', saveConfig);
});

// 保存配置的函数
function saveConfig() {
  const modelUrl = document.getElementById('modelUrl').value.trim();
  const modelName = document.getElementById('modelName').value.trim();
  const apiKey = document.getElementById('apiKey').value.trim();
  
  // 验证输入
  if (!modelUrl || !modelName || !apiKey) {
    alert('请填写所有配置项！');
    return;
  }
  
  // 保存配置到 chrome.storage
  chrome.storage.sync.set({
    modelUrl: modelUrl,
    modelName: modelName,
    apiKey: apiKey
  }, () => {
    // 显示保存成功消息
    const statusMessage = document.getElementById('statusMessage');
    statusMessage.style.display = 'block';
    
    // 2秒后隐藏消息
    setTimeout(() => {
      statusMessage.style.display = 'none';
    }, 2000);
  });
}

